package com.omronservice.app.curve.controller;


import com.omronservice.app.curve.bean.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/DemoInfoController")
public class DemoInfoController {

    @Autowired
    private DemoInfoRepository demoInfoRepository;

    /**
     * 保存数据.
     * @return
     */
    @RequestMapping("/save")
    public String save(){
        // 内存数据库操作
        demoInfoRepository.save(new DemoInfo("title1", "content1"));
        demoInfoRepository.save(new DemoInfo("title2", "content2"));
        demoInfoRepository.save(new DemoInfo("title3", "content3"));
        demoInfoRepository.save(new DemoInfo("title4", "content4"));
        demoInfoRepository.save(new DemoInfo("title5", "content5"));
        return "save ok";
    }

    /**
     * 获取所有数据.
     * @return
     */
    @RequestMapping("/findAll")
    public Iterable<DemoInfo> findAll(){
        // 内存数据库操作
        return demoInfoRepository.findAll();
    }


    private String HOST = "http://localhost:1493/";

    /**
     * 获取所有产线
     * @return
     */
    @RequestMapping("/getAllLine")
    public Object getAllLine(){
         RestTemplate restTemplate1=new RestTemplate();

        JSONObject json = restTemplate1.getForEntity(HOST+"Save_GetLine.aspx", JSONObject.class).getBody();

        return json.toString();
    }

    /**
     * 保存版本
     * @return
     */
    @RequestMapping("/saveVersion")
    public Object saveVersion(@RequestParam(value = "line_id", required = true) String line_id,@RequestParam(value = "version_no", required = true) String version_no){
        RestTemplate restTemplate1=new RestTemplate();
        JSONObject json = restTemplate1.getForEntity(HOST+"Save_SaveLine.aspx?line_id="+line_id+"&version_no="+version_no
                , JSONObject.class).getBody();
        return json.toString();
    }


    /**
     * 获取产线对应版本
     * @return
     */
    @RequestMapping("/getVersion")
    public Object getAllLine(@RequestParam(value = "line_id", required = true) String line_id){
        RestTemplate restTemplate1=new RestTemplate();
        JSONObject json = restTemplate1.getForEntity(HOST+"Select_GetVersion.aspx?line_id="+line_id
                , JSONObject.class).getBody();

        return json.toString();
    }

    /**
     * 获取版本对应设备
     * @return
     */
    @RequestMapping("/getDevices")
    public Object getDevice(@RequestParam(value = "line_id", required = true) String line_id,@RequestParam(value = "version_no", required = true) String version_no){
        RestTemplate restTemplate1=new RestTemplate();
        JSONObject json = restTemplate1.getForEntity(HOST+"Select_GetEquipment.aspx?line_id="+line_id+"&version_no="+version_no
                , JSONObject.class).getBody();

        return json.toString();
    }


    /**
     * 保存配置
     * @return
     */
    @RequestMapping("/saveSettings")
    public Object saveSettings(@RequestParam(value = "line_id", required = true) String line_id,@RequestParam(value = "version_no", required = true) String version_no,
                               @RequestParam(value = "equipment_id", required = true) String[] equipment_id){
        JSONArray eqIds=new JSONArray();
        for(String s:equipment_id){
            eqIds.add(s);
        }

        RestTemplate restTemplate1=new RestTemplate();
        JSONObject json = restTemplate1.getForEntity(HOST+"Select_SaveParameter.aspx?line_id="+line_id+"&version_no="+version_no+"&equipment_id="+eqIds
                , JSONObject.class).getBody();


        return json.toString();
    }

}
